/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.actuserchange.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.actuserchange.entity.ActUserChange;
import com.jeeplus.modules.actuserchange.mapper.ActUserChangeMapper;
import com.jeeplus.modules.actuserchange.entity.ActUserChangeEntry;
import com.jeeplus.modules.actuserchange.mapper.ActUserChangeEntryMapper;

/**
 * 流程人员变更Service
 * @author 流程人员变更
 * @version 2023-10-25
 */
@Service
@Transactional(readOnly = true)
public class ActUserChangeService extends CrudService<ActUserChangeMapper, ActUserChange> {

	@Autowired
	private ActUserChangeEntryMapper actUserChangeEntryMapper;

	public ActUserChange get(String id) {
		ActUserChange actUserChange = super.get(id);
		actUserChange.setActUserChangeEntryList(actUserChangeEntryMapper.findList(new ActUserChangeEntry(actUserChange)));
		return actUserChange;
	}

	public List<ActUserChange> findList(ActUserChange actUserChange) {
		return super.findList(actUserChange);
	}

	public Page<ActUserChange> findPage(Page<ActUserChange> page, ActUserChange actUserChange) {
		return super.findPage(page, actUserChange);
	}

	@Transactional(readOnly = false)
	public void save(ActUserChange actUserChange) {
		super.save(actUserChange);
		for (ActUserChangeEntry actUserChangeEntry : actUserChange.getActUserChangeEntryList()){
			if (actUserChangeEntry.getId() == null){
				continue;
			}
			if (ActUserChangeEntry.DEL_FLAG_NORMAL.equals(actUserChangeEntry.getDelFlag())){
				if (StringUtils.isBlank(actUserChangeEntry.getId())){
					actUserChangeEntry.setUpper(actUserChange);
					actUserChangeEntry.setUser(actUserChange.getUsernew());

					actUserChangeEntry.preInsert();
					actUserChangeEntryMapper.insert(actUserChangeEntry);
				}else{
					actUserChangeEntry.preUpdate();
					actUserChangeEntry.setUser(actUserChange.getUsernew());
					actUserChangeEntryMapper.update(actUserChangeEntry);
				}
			}else{
				actUserChangeEntryMapper.delete(actUserChangeEntry);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(ActUserChange actUserChange) {
		super.delete(actUserChange);
		actUserChangeEntryMapper.delete(new ActUserChangeEntry(actUserChange));
	}

}
